package com.njym.java.security.app.rest;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njym.java.security.app.biz.UserMessageBiz;
import com.njym.java.security.app.entity.FamilyNews;
import com.njym.java.security.app.entity.UserMessage;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("userMessage")
public class UserMessageController extends BaseController<UserMessageBiz,UserMessage> {
    /**
     * 查询历史消息
     * @param from
     * @param to
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(value = "/queryHistory",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<UserMessage> queryHistory(String from, String to,
        @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1")int page){

        List<UserMessage> list = baseBiz.queryHistory(from + to, to + from, page, limit);
        int total = baseBiz.countHistory(from + to, to + from);
        int totalPages = (int) Math.ceil((double) total / limit);

        Map<String, Object> data = new HashMap<>(2);
        data.put("list", list);
        data.put("totalPages", totalPages);
        return new ObjectRestResponse<UserMessage>().rel(true).data(data);

    }
}